<template>
	<view :class="darkMode?'custom-dark':'custom-light'">
		<view class="place-detail">
			<view class="padding-lr padding-tb-xs detail-item">
				<view class="uni-flex uni-row">
					<view class="flex-item-50">
						<uni-tag size="small" :text="formatCheckStatus(item.CheckID).text" :type="formatCheckStatus(item.CheckID).color"
						 :circle="true"></uni-tag>
					</view>
				</view>
			</view>
			<view class="cu-bar detail-item solid-bottom">
				<view class="action">
					<text class="cuIcon-title text-blue"></text>销售订单信息
				</view>
			</view>
			<view class="padding-lr padding-tb-xs detail-item">
				<view class="uni-flex uni-row">
					<view class="flex-item-30">单据号</view>
					<view class="flex-item-70">{{item.BillNO}}</view>
				</view>
			</view>
			<view class="padding-lr padding-tb-xs detail-item">
				<view class="uni-flex uni-row">
					<view class="flex-item-30">日期</view>
					<view class="flex-item-70">{{item.Date|dateFormate}}</view>
				</view>
			</view>
			<view class="padding-lr padding-tb-xs detail-item">
				<view class="uni-flex uni-row">
					<view class="flex-item-30">组织机构</view>
					<view class="flex-item-70" :key="item.OrgObj.ID">{{item.OrgObj.Number}} - {{item.OrgObj.Name}}</view>
				</view>
			</view>
			<view class="padding-lr padding-tb-xs detail-item">
				<view class="uni-flex uni-row">
					<view class="flex-item-30">部门</view>
					<view class="flex-item-70" :key="item.DeptObj.ID">{{item.DeptObj.Number}} - {{item.DeptObj.Name}}</view>
				</view>
			</view>
			<view class="padding-lr padding-tb-xs detail-item">
				<view class="uni-flex uni-row">
					<view class="flex-item-30">业务员</view>
					<view class="flex-item-70" :key="item.EmpObj.ID">{{item.EmpObj.Number}} - {{item.EmpObj.Name}}</view>
				</view>
			</view>
			<view class="padding-lr padding-tb-xs detail-item">
				<view class="uni-flex uni-row">
					<view class="flex-item-30">备注</view>
					<view class="flex-item-70">{{item.Description}}</view>
				</view>
			</view>
			<view class="cu-bar detail-item solid-bottom">
				<view class="action">
					<text class="cuIcon-title text-blue"></text>往来信息
				</view>
			</view>
			<view class="padding-lr padding-tb-xs detail-item">
				<view class="uni-flex uni-row">
					<view class="flex-item-30">客户</view>
					<view class="flex-item-70" :key="item.RelOrgObj.ID">{{item.RelOrgObj.Number}} - {{item.RelOrgObj.Name}}</view>
				</view>
			</view>
			<view class="padding-lr padding-tb-xs detail-item">
				<view class="uni-flex uni-row">
					<view class="flex-item-30">币别</view>
					<view class="flex-item-70" :key="item.CurrencyObj.ID">{{item.CurrencyObj.Number}} - {{item.CurrencyObj.Name}}</view>
				</view>
			</view>
			<view class="cu-bar detail-item solid-bottom">
				<view class="action">
					<text class="cuIcon-title text-orange"></text>订单明细
				</view>
			</view>
			<view v-for="detail in itemdetails" :key="detail.sodid" class="padding-lr padding-tb-xs detail-item solid-bottom">
				<view class="uni-flex uni-row">
					<view class="flex-item-30">品名</view>
					<view class="flex-item-70">{{detail.matnum}} / {{detail.matname}} / {{detail.model}}</view>
				</view>
				<view class="uni-flex uni-row">
					<view class="flex-item-30">单价</view>
					<view class="flex-item-70">{{item.CurrencyObj.Sign}} {{detail.price|round(2)}}</view>
				</view>
				<view class="uni-flex uni-row">
					<view class="flex-item-30">数量</view>
					<view class="flex-item-70">{{detail.curqty|round(2)}} {{detail.unitname}}</view>
				</view>
				<view class="uni-flex uni-row">
					<view class="flex-item-30">金额</view>
					<view class="flex-item-70">{{item.CurrencyObj.Sign}} {{detail.amount|round(2)}}</view>
				</view>
				<view class="uni-flex uni-row">
					<view class="flex-item-30">备注</view>
					<view class="flex-item-70">{{detail.description}}</view>
				</view>
			</view>
			<view class="cu-bar detail-item solid-bottom  margin-top">
				<view class="action">
					<text class="cuIcon-title text-blue"></text>附件
				</view>
			</view>
			<view class="padding-lr padding-tb-xs detail-item solid-bottom">
				<view class="uni-flex uni-row" v-for="atta in itemaccs" :key="atta.accentryid" @tap="filePreview(atta)">
					<view class="flex-item-100">
						<uni-icons color="#007aff" type="paperclip" size="22"></uni-icons><text>{{atta.alisename}}</text>
					</view>
				</view>
			</view>
		</view>
		<view class="goods-carts">
			<uni-goods-nav :options="goodNavOptions" :button-group="goodNavButtonGroup" @click="goodNavOnClick" @buttonClick="goodNavButtonClick" />
		</view>
	</view>
</template>

<script>
	import {
		mapGetters
	} from 'vuex'
	import uniGoodsNav from '@/components/uni-goods-nav/uni-goods-nav.vue'
	import uniTag from '@/components/uni-tag/uni-tag.vue'
	import uniIcons from '@/components/uni-icons/uni-icons.vue'
	import {
		filePreview,
		formatCheckStatus
	} from '@/utils/index.js'
	import {
		formatDateDay
	} from '@/utils/datetime.js'
	export default {
		components: {
			uniGoodsNav,
			uniTag,
			uniIcons,
		},
		computed: {
			...mapGetters(['themeBgColor', 'darkMode']),
		},
		data() {
			return {
				isPass: false,
				_uimainid: 3000882,
				_entityid: 2270092,
				item: {
					CheckID:0,//默认为未审核
					OrgObj: {},
					DeptObj: {},
					EmpObj: {},
					RelOrgObj: {},
					CurrencyObj: {}
				},
				itemdetails: [],
				itemaccs: [],
				goodNavOptions: [],
				goodNavButtonGroup: []
			}
		},
		filters: { //过滤器
			round: function(v, arg1) {
				return _.round(v, arg1);
			},
			dateFormate: function(v) {
				return formatDateDay(v);
			},
		},
		onReady() {
			uni.setNavigationBarTitle({
				title: this.$t('SaleOrderDetail')
			})
			// navBar-bg-color
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: this.themeBgColor,
				animation: {
					duration: 400,
					timingFunc: 'easeIn'
				}
			})
		},
		onLoad(options) {
			this._uimainid = 3000882;
			this._entityid = 2270092;
			let saleorderid = JSON.parse(options.data)
			this.$minApi.GetBillEntity({
				data: {
					_entityid: this._entityid,
					_uimainid: this._uimainid,
					_id: saleorderid,
					_childs: "2270093",
					_rels: "OrganizationID,DepartMentID,EmployeeID,RelatedOrgID,CurrencyID"
				}
			}).then(res => {					
				if (res.Status === 1) {
					let curData = JSON.parse(res.Data);
					this.item = curData._bill;
					this.item.OrgObj = curData._rels_OrganizationID ? (curData._rels_OrganizationID) : {
						ID: 0,
						Number: '',
						Name: ''
					};
					this.item.DeptObj = curData._rels_DepartMentID ? (curData._rels_DepartMentID) : {
						ID: 0,
						Number: '',
						Name: ''
					};
					this.item.EmpObj = curData._rels_EmployeeID ? (curData._rels_EmployeeID) : {
						ID: 0,
						Number: '',
						Name: ''
					};
					this.item.RelOrgObj = curData._rels_RelatedOrgID ? (curData._rels_RelatedOrgID) : {
						ID: 0,
						Number: '',
						Name: ''
					};
					this.item.CurrencyObj = curData._rels_CurrencyID ? (curData._rels_CurrencyID) : {
						ID: 0,
						Number: '',
						Name: '',
						Sign: '￥'
					};
					if (this.item.CheckID>0) {
						this.goodNavOptions = [];
						this.goodNavButtonGroup = [{
							text: '反审核',
							backgroundColor: '#39b54a',
							color: '#fff'
						}];
					} else {
						this.goodNavOptions = [{
							icon: '/static/img/edit.png',
							text: '编辑'
						}];
						this.goodNavButtonGroup = [{
								text: '删除',
								backgroundColor: '#e54d42',
								color: '#fff'
							},
							{
								text: '审核',
								backgroundColor: '#39b54a',
								color: '#fff'
							}
						];
					}
					//获取明细
					this.$minApi.GetDataTable({
						data: {
							_code: "M_SO_002",
							soid: saleorderid
						}
					}).then(res => {
						if (res.Status === 1) {
							this.itemdetails = JSON.parse(res.Data);
						} else {
							this.itemdetails = [];
						}
					}).catch(exp => {
						this.itemdetails = [];
					});
					//获取附件
					if (this.item.AccessoriesID > 0) {
						this.$minApi.GetAccessoriesUrls({
							data: this.item.AccessoriesID
						}).then(res => {
							if (res.Status === 1) {
								this.itemaccs = JSON.parse(res.Data);
							} else {
								this.itemaccs = [];
							}
						}).catch(exp => {
							this.itemaccs = [];
						});
					}
				} else {
					this.item = {};
				}
			}).catch(exp => {
				this.item = {};
			})
		},
		methods: {
			filePreview,
			formatCheckStatus,
			goodNavOnClick(e) {
				if (e.content.text === '编辑') {
					uni.redirectTo({
						url: '/pages/sale/saleorder-edit?data=' + this.item.ID
					})
				}
			},
			goodNavButtonClick(e) {
				if (e.content.text === "审核") {
					this.$minApi.CheckBillEntity({
						data: {
							"_entityid": this._entityid,
							"_uimainid": this._uimainid,
							"_colid": 1871,
							"ids": this.item.ID + "",
							"Description": "同意",
							"Name": "审核"
						}
					}).then(res => {
						if (res.Status === 1) {
							uni.showToast({
								title: `审核成功`,
								icon: 'none'
							});
							uni.redirectTo({
								url: '/pages/sale/saleorder-detail?data=' + this.item.ID
							});
						} else {
							uni.showToast({
								title: `${res.Message}`,
								icon: 'none'
							});
						}
					}).catch(exp => {
						uni.showToast({
							title: `${exp}`,
							icon: 'none'
						});
					});
				} else if (e.content.text === "反审核") {
					this.$minApi.UnCheckBillEntity({
						data: {
							"_entityid": this._entityid,
							"_uimainid": this._uimainid,
							"_colid": 1871,
							"ids": this.item.ID + "",
							"Description": "",
							"Name": "审核"
						}
					}).then(res => {
						if (res.Status === 1) {
							uni.redirectTo({
								url: '/pages/sale/saleorder-detail?data=' + this.item.ID
							});
						} else {

						}
					}).catch(exp => {

					});
				} else if (e.content.text === "删除") {
					var that = this;
					uni.showModal({  
						title:"删除",// 标题  
						content:"确认删除当前单据",// 内容  
						showCancel:true ,// 可以不要  
						success: function (res) {
							if (res.confirm) {
								that.$minApi.DelBillEntity({
									data: {
										"_entityid": that._entityid,
										"_uimainid": that._uimainid,
										"ids": that.item.ID + "",
									}
								}).then(res => {
									if (res.Status === 1) {
										uni.showToast({
											title: `删除成功`,
											icon: 'none'
										});
										uni.redirectTo({
											url: '/pages/sale/saleorder-list',
										})
									} else {}
								}).catch(exp => {
									uni.showToast({
										title: `${exp}`,
										icon: 'none'
									});
								});
												
							}
							 else if (res.cancel) {
						
							}
						}
					});
				}
			},
		}
	}
</script>

<style>
	.place-detail {
		/* margin: 27rpx 20rpx 104rpx 20rpx; */
		padding-bottom: 104rpx;
	}

	/* .place-detail .uni-row {
		margin: 5rpx 0;
	} */
</style>
